package classes.Commands
{
	import components.editor.c_baseEditor;
	import components.editor.c_baseElement;
	import components.editor.c_bitmapElement;
	import components.editor.c_multiLayerElement;
	
	import flash.display.DisplayObject;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class c_multylayerDivideCommand extends c_commandFromEditor {
		private var _layers		: Array;
		private var _index		: int;
		public function c_multylayerDivideCommand(a_editor: c_baseEditor, a_element: c_multiLayerElement) {
			super(a_editor, a_element);
		}
		
		protected function get mlElement(): c_multiLayerElement {
			return super.element as c_multiLayerElement;
		}
		
		override public function Execute():void {
			super.Execute();
			_index = element.index;
			if (!_layers) {
				_layers = new Array();
				var matrix: Matrix = mlElement.matrix;
				for (var i: int=0; i<mlElement.layer3D.numChildren; i++) {
					var image: DisplayObject = mlElement.layer3D.getChildAt(i);
					var rect: Rectangle = image.getBounds(mlElement.layer3D);
					rect.x += mlElement.x;
					rect.y += mlElement.y;
					var oneElem: c_bitmapElement 	= new c_bitmapElement(editor.imageLayer, rect, image);
					_layers.push(oneElem);
				};
			} else {
				_layers.forEach(function(item: c_bitmapElement, i: int, arr: Array): void {
					item.setParent(editor.imageLayer);
				});
			}
			element.remove();
		}
		
		override public function unExecute():void {
			_layers.forEach(function(item: c_bitmapElement, i: int, arr: Array): void {
				item.remove();
			});
			element.setParent(editor.imageLayer, _index);
			super.unExecute();
		}
	}
}